package com.crlui.core.state
{
	import flash.utils.Dictionary;

	public class StateMachine
	{
		protected var states:Dictionary;
		
		protected var currentState:IState;
		public function StateMachine()
		{
			states=new Dictionary();
		}
		
		
		public function add(state:IState):void{
			states[state.type]=state;
		}
		
		public function remove(name:String):IState{
			var state:IState=state[name];
			
			if(state){
				state[name]=null;
				delete state[name];
			}
			return state;
		}
		
		
		public function set state(name:String):void{
			var state:IState=state[name];
			
			if(currentState){
				currentState.end();
			}
			
			if(state.initialized)state.initialize();
			state.start();
		}
		
	}
}